package cn.dnui_xqh414.dnuifood_16110100414;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Objects;
import java.util.regex.Pattern;

import cn.dnui_xqh414.dnuifood_16110100414.bean.Userbean;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.UserModel;

public class LoginActivity extends BaseActivity implements OnRetrofitListener<Userbean>, View.OnClickListener  {


    UserModel model;

    Button relogin ;
    TextInputEditText username, password, Mobile;
    TextInputLayout tt_userName,tt_password, tt_Mobile;
    ImageButton imgbt_return;
    CheckBox cb_remember;

    private String user,pass;
    private String susername,spassword;

    private SharedPreferences userInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    void initViews() {

        userInfo = getSharedPreferences("userinfo",MODE_PRIVATE);

        setContentView(R.layout.activity_login);

        imgbt_return = findViewById(R.id.imgbt_return);
        relogin = findViewById(R.id.bt_relogin);
        username = findViewById(R.id.et_username);
        password = findViewById(R.id.et_password);
        tt_password = findViewById(R.id.tt_password);
        tt_userName = findViewById(R.id.tt_userName);
        cb_remember = findViewById(R.id.remember);


        if (!userInfo.getString("username","").equals("")){
            username.setText(userInfo.getString("username",""));
            password.setText(userInfo.getString("password",""));
            cb_remember.setChecked(true);
        }
        susername = getIntent().getStringExtra("rg_username");
        spassword = getIntent().getStringExtra("rg_password");
        if (susername != null)
            username.setText(susername);
        if (spassword != null)
            password.setText(spassword);
    }

    @Override
    void initEvents() {

        relogin.setOnClickListener(this);

        imgbt_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    void initData() {
    }

    @Override
    public void onClick(View v) {

        user = Objects.requireNonNull(username.getText()).toString();
        pass = Objects.requireNonNull(password.getText()).toString();

        tt_userName.setErrorEnabled(false);
        tt_password.setErrorEnabled(false);

        if (vailUsername(user)&&vailPassword(pass)){
            model = new UserModel();
            model.login(user, pass, this);
        }
    }

    @Override
    public void onSuccess(Userbean object, int flag) {

        if (flag == 1&&object.getUserid()!=0){
            Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor editor = userInfo.edit();
            editor.putInt("userid", object.getUserid());
            if (cb_remember.isChecked()){
                editor.putString("username", String.valueOf(username.getText()));
                editor.putString("password",String.valueOf(password.getText()));
                editor.commit();
            }else {
                editor.putString("username", "");
                editor.putString("password", "");
                editor.commit();
            }
            Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
            intent.putExtra("userid",object.getUserid());
            startActivity(intent);

        }else {
            Toast.makeText(this, "username or password wrong", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFailure(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private void showError(TextInputLayout layout, String error){
        layout.setError(error);
        Objects.requireNonNull(layout.getEditText()).setFocusable(true);
        layout.getEditText().setFocusableInTouchMode(true);
        layout.getEditText().requestFocus();
    }

    private boolean vailUsername(String userName){
        String p_username = "[a-zA-Z0-9_]{1,16}";

        if (userName.isEmpty()){
            showError(tt_userName, "please input the username");
            return false;
        }
        if (!Pattern.matches(p_username,userName)){
            showError(tt_userName, "username must use a-z, 0-9, underline and keep character in the 16");
            return false;
        }
        return true;
    }

    private boolean vailPassword(String password){
        String p_password = "[a-zA-Z0-9]{1,18}";

        if (password.isEmpty()){
            showError(tt_password, "please input the password");
            return false;
        }
        if(password.length()>18||password.length()<6){
            showError(tt_password, "password should between 18 and 6 characters");
            return false;
        }
        if (!Pattern.matches(p_password,password)){
            showError(tt_password, "password must use a-z, 0-9");
            return false;
        }
        return true;
    }


}
